package com.zx.map.service.impl;

import com.zx.map.dao.SessionInfoDao;
import com.zx.map.dao.model.SessionInfo;
import com.zx.map.dao.model.SessionInfoExample;
import com.zx.map.service.SessionInfoService;
import com.zx.map.service.model.SessionInfoModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("sessionInfoService")
public class SessionInfoServiceImpl implements SessionInfoService {

	@Autowired
	private SessionInfoDao dao;
                                            	/**
	 * 根据Model查找列表
	 */
	public List<SessionInfoModel> getListByModel(SessionInfoModel model) throws DataAccessException {
		SessionInfoExample example = new SessionInfoExample();
		SessionInfoExample.Criteria criteria = example.createCriteria();
		criteria.andExpireDateGreaterThan(new Date());//查询系统当前时间仍然生效的数据
        if(StringUtils.isNotBlank(model.getSessionId()))
        {
             criteria.andSessionIdEqualTo(model.getSessionId());
        }
		List<SessionInfo> list = dao.getListByExample(example);
		List<SessionInfoModel> modelList = new ArrayList<SessionInfoModel>();
		for (SessionInfo o : list) {
			SessionInfoModel temp = new SessionInfoModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}

	/**
	 * 新增
	 */
	public String add(SessionInfoModel model) throws DataAccessException {
		SessionInfo temp = new SessionInfo();
		BeanUtils.copyProperties(model, temp);
		String key = dao.add(temp);
		return key;
	}

	/**
	 * 修改
	 */
	public String update(SessionInfoModel model) throws DataAccessException {
		SessionInfo temp = new SessionInfo();
		BeanUtils.copyProperties(model, temp);
		dao.update(temp);
		return model.getSessionId();
	}

	/**
	 * 删除
	 */
	public void delete(String loginKey) throws DataAccessException {
		SessionInfoExample example = new SessionInfoExample();
		SessionInfoExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(loginKey))
        {
             criteria.andLoginKeyEqualTo(loginKey);
             dao.deleteByExample(example);
        }		
	}
}
